﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Security.Principal;
using GestionePolveriLib.BL.Reports;

namespace GestionePolveri2.Reports
{
    public partial class ReportViewerDialog : Form
    {
        private string m_report = string.Empty;
        private Report51 m_datasource = null;

        public ReportViewer ReportViewer
        {
            get { return this.reportViewer1; }
        }

        public string Report
        {
            get { return m_report; }
            set { m_report = value; }
        }

        public Report51 Datasource
        {
            get { return m_datasource; }
            set { m_datasource = value; }
        }

        public ReportViewerDialog()
        {
            InitializeComponent();
        }

        private void ReportViewerDialog_Load(object sender, EventArgs e)
        {
            this.reportViewer1.RefreshReport();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void ShowReport()
        {
            if (m_report == string.Empty)
            {
                MessageBox.Show("Indicare il nome del report !", "Report viewer Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (m_datasource != null)
            {
                //Microsoft.Reporting.WinForms.ReportViewer reportViewer = this.reportViewer1;

                reportViewer1.LocalReport.DataSources.Clear();

                // Set Processing Mode
                reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;

                // Set RDL file
                reportViewer1.LocalReport.ReportPath = string.Format(@"{0}\Reports\{1}.rdlc", Application.StartupPath, m_report);

                IPrincipal m_currentUser = System.Threading.Thread.CurrentPrincipal;

                List<ReportParameter> parameters = new List<ReportParameter>();
                parameters.Add(new ReportParameter("TotaleEuro", m_datasource.TotaleAmministrazione.TotaleEuro.ToString()));
                parameters.Add(new ReportParameter("TotaleKg", m_datasource.TotaleAmministrazione.TotaleKg.ToString()));
                this.reportViewer1.LocalReport.SetParameters(parameters);

                // Supply data corresponding to each report data source.
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportViewerDialogData", m_datasource.LottiSelezionati));
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("TotaleXFamigliaDataset", m_datasource.TotaleXFamiglia));

                // Process and render the report
                reportViewer1.RefreshReport();
            }
            else
            {
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportViewerDialogData", m_datasource));
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.RefreshReport();

                MessageBox.Show("Nessun dato trovato!");
            }

            this.Show();
        }
    }
}
